package commons.entity;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyMouseSwitcher {
    public static void transferMouse(String type){
        // 获取Spring容器上下文
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        // 获取MyService的原始bean
        InMyMouse im = context.getBean(InMyMouse.class);
        // 关闭原有的bean工厂，以允许动态注册新的bean定义
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) context.getBeanFactory();
        // 创建一个新的BeanDefinition
        RootBeanDefinition newBeanDefinition;
        if(Constant.MOUSE_ROBOT == type){
            newBeanDefinition = new RootBeanDefinition(MouseRobot.class);
        }else if(Constant.MOUSE_HIVK == type){
            newBeanDefinition = new RootBeanDefinition(MouseHIKV.class);
        }else{
            newBeanDefinition = new RootBeanDefinition(MouseRobot.class);
        }
        registry.registerBeanDefinition("myService", newBeanDefinition);
    }
}
